﻿///////////////////////////////////////////////////////////////////////////////
// File:        BigNumber.hpp
// Author:      671643387@qq.com
// Date:        2016年1月5日 下午5:47:38
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef CRYPTO_BIGNUMBER_HPP_
#define CRYPTO_BIGNUMBER_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <fog/Typedef.hpp>

struct bignum_st;

namespace fog
{
    namespace crypt
    {
        class BigNumber
        {
        public:
            BigNumber(void);
            BigNumber(const BigNumber& bn);
            BigNumber(uint32_t);
            ~BigNumber(void);

        public:
            void setUInt32(uint32_t);
            void setUInt64(uint64_t);
            void setBinary(const uint8_t*, int);
            void setHexStr(const char*);
            void setRand(int);

            static void free(void*);

            BigNumber operator=(const BigNumber&);

            BigNumber operator+=(const BigNumber&);
            BigNumber operator+(const BigNumber& x)
            {
                BigNumber t(*this);
                return t += x;
            }

            BigNumber operator-=(const BigNumber&);
            BigNumber operator-(const BigNumber& x)
            {
                BigNumber t(*this);
                return t -= x;
            }

            BigNumber operator*=(const BigNumber&);
            BigNumber operator*(const BigNumber& x)
            {
                BigNumber t(*this);
                return t *= x;
            }

            BigNumber operator/=(const BigNumber&);
            BigNumber operator/(const BigNumber& x)
            {
                BigNumber t(*this);
                return t /= x;
            }

            BigNumber operator%=(const BigNumber&);
            BigNumber operator%(const BigNumber& x)
            {
                BigNumber t(*this);
                return t %= x;
            }

            bool isZero(void) const;
            BigNumber modExp(const BigNumber&, const BigNumber&);
            BigNumber exp(const BigNumber&);
            int getNumBytes(void);
            struct bignum_st* BN(void) { return bn_; }
            uint32_t asUInt32(void);
            BYTE_t* asByteArray(int minSize = 0);
            const char* asHexStr(void);
            const char* asDecStr(void);

        private:
            struct bignum_st *bn_;
            BYTE_t           *array_;
        };
    }
}

#endif /* CRYPTO_BIGNUMBER_HPP_ */
